package com.ddf.norman.controller;

import com.ddf.norman.util.RegexUtils;
import com.ddf.publics.entity.QQUserInfo;
import com.ddf.publics.entity.User;
import com.ddf.publics.service.LoginService;
import com.ddf.publics.service.QQService;
import com.ddf.publics.service.UserService;
import com.ddf.publics.utils.RedisUtil;
import com.ddf.publics.utils.VerificationCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class QQLoginController {
    //注入服务层
    @Autowired
    private QQService qqService;
    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private VerificationCodeUtil verificationCodeUtil;

    //注入工具类
    @Autowired
    private RedisUtil redisUtil;

    /**
     * @description QQ登录的回调地址
     * @author norman
     * @date 2020/7/5 19:13
     * @params [request, response, session, code]
     * @return java.lang.String
     */
    @RequestMapping(value = "/qqLogin")
    public String qqLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session, String code) throws Exception {
        //获取token
        Map<String, Object> qqProperties = qqService.getToken(code);
        String openId = qqService.getOpenId(qqProperties);
        qqProperties.put("openId",openId);

        //根据openId加载userDetails
        User user = userService.qqLogin(openId);
        if (user == null) { //如果是新用户
            session.setAttribute("qqProperties",qqProperties);
            return "redirect:/qqBinding";
        }
        qqService.securityLogin(user,request);
        return "redirect:/index";
    }

    /**
     * @description 请求绑定手机号页面
     * @author norman
     * @date 2020/7/5 19:14
     * @params [session]
     * @return java.lang.String
     */
    @RequestMapping("/qqBinding")
    public String qqBinding(HttpSession session) {
        Object qqProperties = session.getAttribute("qqProperties");
        if (qqProperties == null || "".equals(qqProperties)) {
            return "redirect:/index";
        }
        return "qqBinding";
    }

    /**
     * @description 发送绑定手机号的验证码
     * @author norman
     * @date 2020/7/3 16:18
     * @params [phone]
     * @return java.lang.String
     */
    @ResponseBody
    @PostMapping("/sendBindingCode")
    public String sendBindingCode(String phone) {

        User user = loginService.queryByPhone(phone);
        if (user == null) {
            return "该手机未注册叮叮金融";
        }
        if (RegexUtils.checkPhone(phone)) {
            String code = String.valueOf((int)((Math.random()*9+1)*100000));
            System.out.println("code = " + code);
            String message = verificationCodeUtil.sendCode(phone,VerificationCodeUtil.ALI_TEMPLATE_QQ_BINDING,code);
            if ("OK".equals(message)) {
                redisUtil.set("bindingCode:" + phone,code,5 * 60);
            } else {
                System.out.println("message = " + message);
                return "服务器繁忙";
            }
        } else {
            return "手机号格式错误";
        }
        return "success";
    }

    /**
     * @description QQ用户绑定叮叮金融的账号
     * @author norman
     * @date 2020/7/5 19:15
     * @params [phone, ver, request, response, session]
     * @return java.lang.String
     */
    @ResponseBody
    @PostMapping("/bindingUser")
    public String bindingUser(String phone, String ver,HttpServletRequest request, HttpSession session) throws Exception {
        if (phone == null || !RegexUtils.checkPhone(phone)) {
            return "手机号格式错误";
        }
        User user = loginService.queryByPhone(phone);
        if (user == null) {
            return "该手机未注册叮叮金融";
        }

        Object code = redisUtil.get("bindingCode:"+phone);
        System.out.println("code = " + code);
        if (code == null || !ver.equals(code)) {
            return "验证码错误";
        }

        //获取数据
        Map<String, Object> qqProperties = (Map<String, Object>) session.getAttribute("qqProperties");
        String openId = qqService.getOpenId(qqProperties);
        qqProperties.put("openId",openId);
        QQUserInfo qqUserInfo =  qqService.getUserInfo(qqProperties);

        //把从qq里获取到的用户信息更新到实体类
        User dataUser = new User(user.getId());
        dataUser.setOpenId(openId);
        if ("/img/defaultFace.png".equals(user.getFace())){
            dataUser.setFace(qqUserInfo.getFigureurl_qq_2());
        }

        //更新到数据库
        userService.update(dataUser);

        qqService.securityLogin(user,request);
        return "success";
    }
}
